package com.launch.carmanager.data;

import android.content.Context;
import android.content.SharedPreferences;

import com.github.pwittchen.prefser.library.Prefser;

import java.util.HashSet;
import java.util.Set;

/**
 * SharedPreferences
 * Created by ganquan on 2019/3/28 09:37
 */
public class PrefserHelper {

    private volatile static Prefser sPrefser;
    private static String userToken = null;
    private static String userName = null;

    private static final String KEY_COOKIES = "cookies";
    private static final String KEY_USER_TOKEN = "user_token";
    private static final String KEY_USER_NAME = "user_name";
    private static final String KEY_LOGGED_IN = "logged_in";

    public PrefserHelper() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    public static void initPrefser(Context context) {
        if (sPrefser == null) {
            sPrefser = new Prefser(context.getApplicationContext());
        }
    }

    /**
     * 清空默认的SharedPreferences文件
     */
    public static void clearPrefser() {
        sPrefser.clear();
    }

    public static void removeKey(String key) {
        sPrefser.remove(key);
    }

    /**
     * 获取自定义的SharedPreferences
     *
     * @param sp SharedPreferences文件
     */
    public static Prefser getPrefser(SharedPreferences sp) {
        return new Prefser(sp);
    }

    @SuppressWarnings("unchecked")
    public static HashSet<String> getCookies() {
        return (HashSet<String>) sPrefser.get(KEY_COOKIES, Set.class, new HashSet<>());
    }

    public static void saveCookies(HashSet<String> cookies) {
        sPrefser.put(KEY_COOKIES, cookies);
    }

    /**
     * 是否已经登录
     */
    public static boolean isLoggedIn() {
        return sPrefser.get(KEY_LOGGED_IN, Boolean.class, false);
    }

    /**
     * 设置是否已经登录
     */
    public static void setLoggedIn(boolean loggedIn) {
        sPrefser.put(KEY_LOGGED_IN, loggedIn);
    }

    /**
     * 保存用户名
     *
     * @param userName 用户名
     */
    public static void saveUserName(String userName) {
        PrefserHelper.userName = userName;
        sPrefser.put(KEY_USER_NAME, userName);
    }

    /**
     * 获取用户名
     */
    public static synchronized String getUserName() {
        if (userName == null) {
            synchronized (PrefserHelper.class) {
                if (userName == null) {
                    return userName = sPrefser.get(KEY_USER_NAME, String.class, "");
                }
            }
        }
        return userName;
    }

    /**
     * 获取用户的密钥
     */
    public static String getUserKey() {
        if (userToken == null) {
            synchronized (PrefserHelper.class) {
                if (userToken == null) {
                    return userToken = sPrefser.get(KEY_USER_TOKEN, String.class, "");
                }
            }
        }
        return userToken;
    }

    /**
     * 保存用户密钥
     *
     * @param userKey 用户密钥
     */
    public static void saveUserKey(String userKey) {
        PrefserHelper.userToken = userKey;
        sPrefser.put(KEY_USER_TOKEN, userKey);
    }
}
